package com.ikangtai.shecare.common.db.sync;


import android.content.Context;

import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.db.DBManager;
import com.ikangtai.shecare.common.db.DatabaseHelper;
import com.ikangtai.shecare.common.eventBusMsg.sync.SyncALDataMsg;
import com.ikangtai.shecare.common.eventBusMsg.sync.SyncALDataMsg1;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.OKHttpUtil;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.simple.eventbus.EventBus;

import java.io.IOException;

public class SyncALData1 {
    private Context context;
    private SyncALDataMsg1 syncALDataMsg;

    public SyncALData1(Context context, SyncALDataMsg1 syncALDataMsg) {
        this.context = context;
        this.syncALDataMsg = syncALDataMsg;
        EventBus.getDefault().register(this);
    }

    public void SyncALDataWithNetwork(){
        final DBManager dbManager = new DBManager(context);

        JSONObject unSyncedALUserData = dbManager.getUnSyncedALUserData(App.userName);
        JSONArray unSyncedALDayOutput = dbManager.getUnSyncedALDayOutput(App.userName);
        JSONArray unSyncedALCycleOutput = dbManager.getUnSyncedALCycleOutput(App.userName);

        if (unSyncedALUserData == null && unSyncedALDayOutput.length() == 0 && unSyncedALCycleOutput.length() == 0){
            EventBus.getDefault().post(syncALDataMsg);
        } else {
            try {
                OKHttpUtil okHttpClient = new OKHttpUtil(context);
                String appendUrl = "cycle/replaceCycle.json";

                AppLog.i("ALDataWithNetwork is syncing ...!");

                okHttpClient.add("authToken", App.authToken);
                okHttpClient.add("cycleData", unSyncedALCycleOutput);
                okHttpClient.add("userData", unSyncedALDayOutput);
                okHttpClient.add("userInfo", unSyncedALUserData);

                okHttpClient.postAsync(appendUrl, okHttpClient.new BaseCallBack() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        super.onFailure(request, e);
                        EventBus.getDefault().post(syncALDataMsg);
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        int respCode = 0;

                        try {
                            JSONObject respJSONObj = new JSONObject(response.body().string());
                            respCode = respJSONObj.getInt("code");

                            if (App.HTTP_RESP_OK == respCode){
                                dbManager.updateDBSyncFlag(DatabaseHelper.AL_USERDATA_TABLE_NAME, App.userName);
                                dbManager.updateDBSyncFlag(DatabaseHelper.AL_DAYOUTPUT_TABLE_NAME, App.userName);
                                dbManager.updateDBSyncFlag(DatabaseHelper.AL_CYCLEOUTPUT_TABLE_NAME, App.userName);
                                AppLog.i("SyncALDataWithNetwork onResponse success!");
                            } else if (respCode == App.HTTP_RESP_TOKEN_TIME_OUT) {
                                OKHttpUtil.setNewToken();
                                SyncALDataWithNetwork();
                            } else {
                                AppLog.i("SyncALDataWithNetwork onResponse failed! respCode is:" + respCode);
                            }
                        } catch (JSONException e) {
                            AppLog.i("SyncALDataWithNetwork JSONException!");
                            e.printStackTrace();
                        }

                        if (respCode != App.HTTP_RESP_TOKEN_TIME_OUT)
                            EventBus.getDefault().post(syncALDataMsg);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
                EventBus.getDefault().post(syncALDataMsg);
            }
        }
    }
}
